﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinformText4
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        public Form2(int id, string title,string author,string createdTime)
        {
            InitializeComponent();
            this.id = id;
            textBox1.Text = title;
            textBox2.Text = author;
            textBox3.Text = (string)createdTime;

        }

        private int id;


        private void Form2_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            var name = textBox1.Text;
            var title = textBox2.Text;
            //更新
            if (id > 0)
            {
                if (!string.IsNullOrEmpty(name))
                {
                    var sql = string.Format("update Articles set Title='{0}',Author='{1}'where Id={2}", name,title, this.id);
                    var res = DbHelper.AddOrEditSave(sql);
                    if (res == 1)
                    {
                        MessageBox.Show("修改成功！", "提示");
                        this.DialogResult = DialogResult.Yes;
                        this.Close();
                    }
                }
            }
            //新增
            else
            {




                if (!string.IsNullOrEmpty(name))
                {
                    var sql = string.Format("insert into Articles(Title,Author)values('{0}','{1}')", name,title);
                    var res = DbHelper.AddOrEditSave(sql);
                    if (res == 1)
                    {
                        MessageBox.Show("添加成功！", "提示");
                        this.DialogResult = DialogResult.Yes;
                        this.Close();
                    }
                    
                }
                else
                {
                    MessageBox.Show("学生姓名不能为空！", "提示");

                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.No;
            this.Close();
        }
    }
}
